﻿using System;
using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    /// <summary>
    /// 管理员service
    /// </summary>
    public class AdminService
    {
        private static readonly IUserDAL dal = DALFactory.DataAccess.createUserDAL();

        //管理员登陆
        public Hashtable login(string username, string password)
        {
            Hashtable ht = new Hashtable();
            User u = dal.getUserByEmail(username);

            if (u == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "账号不存在");
                return ht;
            }
            if (u.password != password)
            {
                ht.Add("result", "0");
                ht.Add("message", "密码不正确");
                return ht;
            }
            if (u.status != 2 && u.status != 3)
            {
                ht.Add("result", "0");
                ht.Add("message", "亲,你权限不够哦~~");
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "登陆成功");
            ht.Add("admin", u);
            return ht;
        }

        //得到所有用户
        public IList<User> getAllUser()
        {
            IList<User> ul = dal.getUser("from User u");
            return ul;
        }

        //得到正常用户
        public IList<User> getNormalUser()
        {
            IList<User> ul = dal.getUser("from User u where u.status=1");
            return ul;
        }

        //得到ban用户
        public IList<User> getBannedUser()
        {
            IList<User> ul = dal.getUser("from User u where u.status=0");
            return ul;
        }

        //添加用户
        public Hashtable addUser(string name, string username, string password)
        {
            Hashtable ht = new Hashtable();
            //1. 检测用户名是否存在
            User u = dal.getUserByEmail(username);
            if (u != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "该邮箱已被注册");
                return ht;
            }
            u = new User()
            {
                name = name,
                username = username,
                password = password,
                status = 1 //正常
            };
            dal.saveUser(u);
            ht.Add("result", "1");
            ht.Add("message", "添加成功");
            return ht;
        }

        //添加用户
        public Hashtable addUser(string name, string username, string password, int departmentId)
        {
            Hashtable ht = new Hashtable();
            //1. 检测用户名是否存在
            User u = dal.getUserByEmail(username);
            if (u != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "该邮箱已被注册");
                return ht;
            }
            u = new User()
            {
                name = name,
                username = username,
                password = password,
                status = 1, //正常
                department = new DepartmentService().getDepartmentById(departmentId) /* 所属部门 */
            };
            dal.saveUser(u);
            ht.Add("result", "1");
            ht.Add("message", "添加成功");
            return ht;
        }

        //更改为普通管理员
        public Hashtable changeToAdmin(int id)
        {
            Hashtable ht = new Hashtable();
            User u = dal.getUserById(id);
            if (u == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            if (u.status == 2)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户已经是管理员");
                return ht;
            }
            if (u.status == 3)
            {
                ht.Add("result", "0");
                ht.Add("message", "超级管理员不能更改");
                return ht;
            }
            u.status = 2;
            try
            {
                dal.updateUser(u);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "添加管理员成功");
            return ht;
        }

        //添加普通管理员
        public Hashtable addCommonManager(string name, string username, string password)
        {
            Hashtable ht = new Hashtable();
            //1. 检测用户名是否存在
            User u = dal.getUserByEmail(username);
            if (u != null)
            {
                ht.Add("result", "0");
                ht.Add("message", "该邮箱已被注册");
                return ht;
            }
            u = new User()
            {
                name = name,
                username = username,
                password = password,
                status = 2 //管理员
            };
            dal.saveUser(u);
            ht.Add("result", "1");
            ht.Add("message", "添加成功");
            return ht;
        }

        //得到管理组用户
        public IList<User> getAdminUser()
        {
            IList<User> ul = dal.getUser("from User u where u.status=2");
            return ul;
        }

        //冻结用户
        public Hashtable freezeUser(int userId)
        {
            Hashtable ht = new Hashtable();
            User u = dal.getUserById(userId);
            if (u == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            u.status = 0;
            try
            {
                dal.updateUser(u);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "冻结成功");
            return ht;
        }

        //恢复用户
        public Hashtable recoverUser(int userId)
        {
            Hashtable ht = new Hashtable();
            User u = dal.getUserById(userId);
            if (u == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            u.status = 1;
            try
            {
                dal.updateUser(u);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "恢复成功");
            return ht;
        }

        //删除管理员
        public Hashtable deleteAdmin(int userId)
        {
            Hashtable ht = new Hashtable();
            User u = dal.getUserById(userId);
            if (u == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "用户不存在");
                return ht;
            }
            //根据用户删除权限
            new UserActionService().deleteUserAllAction(userId);
            u.status = 1;
            try
            {
                dal.updateUser(u);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }

            ht.Add("result", "1");
            ht.Add("message", "删除成功");
            return ht;
        }

    }
}
